package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.FrameLayout
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.contrarywind.view.WheelView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.CityEntity
import www.wen.com.common.lib.data.entities.CustomAreaEntity
import www.wen.com.common.lib.extension.getColorAgency
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.ResultUiCallBack


/**
 * @author WEN
 * @Description:
 * @date 2021/4/25 21:35
 */
class BottomAreaSelectPopupView(
        context: Context,
        private var province: String? = null,
        private var city: String? = null,
        private var options1Items: List<CityEntity.Content> = mutableListOf(),
        private val options2Items: MutableList<MutableList<CityEntity.Content>> = mutableListOf(),
        private val options3Items: MutableList<MutableList<MutableList<CityEntity.Content>>> = mutableListOf(),
        private val resultCallBack: ResultUiCallBack<CustomAreaEntity>? = null,
) : BottomPopupView(context), View.OnClickListener {

    private lateinit var mOptions: OptionsPickerView<CityEntity.Content>

    private var mAddress: CustomAreaEntity? = null

    private lateinit var mTvOK: View

    private lateinit var mTvToday: View

    private lateinit var mTimePickerContainer: FrameLayout

    private lateinit var mTimePickerInnerContainer: FrameLayout

    override fun getImplLayoutId(): Int = R.layout.popupview_area_select

    override fun getMaxHeight(): Int = (XPopupUtils.getScreenHeight(context) * 0.45f).toInt()

    override fun onCreate() {
        super.onCreate()

        mTvToday = findViewById(R.id.tvToday)
        mTvOK = findViewById(R.id.tvOK)
        mTimePickerContainer = findViewById(R.id.timePickerContainer)
        mTimePickerInnerContainer = findViewById(R.id.timePickerInnerContainer)


        mOptions = OptionsPickerBuilder(context) { options1, options2, options3, v ->
            val opt1tx = if (options1Items.isNotEmpty()) options1Items[options1].pickerViewText else ""
            val opt2tx: String = if (options2Items.size > 0 && options2Items[options1].size > 0) options2Items[options1][options2].name else ""
            val opt3tx: String = if (options2Items.size > 0 && options3Items[options1].size > 0 && options3Items[options1][options2].size > 0) options3Items[options1][options2][options3].name else ""
            mAddress = CustomAreaEntity(opt1tx, opt2tx, "")
        }
                .setLineSpacingMultiplier(2f)
                .setDividerColor(Color.BLACK)
                .setLayoutRes(R.layout.pickerview_area) {}
                .setContentTextSize(15)
                .setItemVisibleCount(12)
                .setDecorView(mTimePickerInnerContainer)
                .setDividerType(WheelView.DividerType.FILL)
                .setOutSideColor(getColorAgency(R.color.colorFFFFFF))
                .setBgColor(getColorAgency(R.color.color1B1D22))
                .setTextColorCenter(getColorAgency(R.color.colorFFFFFF))
                .build()
        mOptions.setPicker(options1Items, options2Items)

        if (province != null && city != null) {
            val index1 = options1Items.indexOfFirst { it.name == province }
            var index2 = -1
            if (index1 != -1) {
                index2 = options2Items[index1].indexOfFirst { it.name == city }
            }
            if (index2 != -1) {
                mOptions.setSelectOptions(index1, index2)
            }
        }

        mOptions.show(false)


        mOptions.returnData()
        setOnClickListenerAgency(this, mTvToday, mTvOK)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tvToday -> {
            }
            R.id.tvOK -> {
                mOptions.returnData()
                resultCallBack?.invoke(v, mAddress!!, this)
            }
        }
        dismiss()
    }
}